package com.caowj.composedemo.activity

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.caowj.composedemo.R
import com.caowj.composedemo.ui.theme.ComposeDemoTheme

/**
 * 列表与列表点击事件
 * https://www.jianshu.com/p/2909234aec01
 */
class ListActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            _DefaultPreview()
        }
    }
}

data class Message2(val name: String, val othername: String, val phone: String)

@Composable
fun _ShowView(msg: Message2) {
    ComposeDemoTheme {
        Row(modifier = Modifier.padding(all = 8.dp)) {
            Surface(
                modifier = Modifier.size(60.dp),
                shape = CircleShape
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_launcher_background),
                    contentDescription = "联系人照片",
                    modifier = Modifier
                        .size(60.dp)
                        .scale(1.0F),
                    contentScale = ContentScale.FillBounds
                )
            }
            Spacer(modifier = Modifier.width(8.dp))
            var isExpand by remember {
                mutableStateOf(false)
            }//创建一个开关值赋值为FALSE，委托remeber去记住这个开关值
            Column(modifier = Modifier.clickable { isExpand = !isExpand }) {
                Text(text = "作者： ${msg.name}!", fontSize = 14.sp)
                Text(text = "时间：${msg.othername}", fontSize = 14.sp)
                Text(
                    text = "主要成就：${msg.phone}",
                    fontSize = 14.sp,
                    color = MaterialTheme.colors.secondaryVariant,
                    maxLines = if (isExpand) Int.MAX_VALUE else 1,//如果开关值为TRUE展开全部，否则只显示一行
                    style = MaterialTheme.typography.body2
                )
            }
        }
    }

}

@Composable
fun _SampleRecycleview(msgList: MutableList<Message2>) {
    LazyColumn {
        items(msgList) { message2 ->
            _ShowView(msg = message2)
        }
    }
}

@Preview(showBackground = true)
@Composable
fun _DefaultPreview() {
    val msgList = mutableListOf<Message2>()
    for (i in 0..20) {
        val msg = Message2(
            "鲁迅",
            "1881年09月25日",
            "鲁迅作品题材广泛，形式多样灵活，风格鲜明独特，语言幽默。在他55年的人生中，创作的作品，体裁涉及小说、杂文、散文、诗歌等。有《鲁迅全集》二十卷1000余万字传世。在中华人民共和国成立后，其多篇作品被选入中小学语文教材，对新中国的语言和文学有着深远的影响。\n" +
                    "\n" +
                    "鲁迅的作品主要以小说、杂文为主，代表作有：小说集《呐喊》《彷徨》《故事新编》等 ；散文集《朝花夕拾》；散文诗集《野草》；杂文集《坟》《热风》《华盖集》《华盖集续编》《南腔北调集》《三闲集》《二心集》《而已集》《且介亭杂文》等。他的作品有数十篇被选入中、小学语文课本，并有多部小说被先后改编成电影。其作品对于五四运动以后的中国文学产生了深刻的影响。 "
        )
        msgList.add(msg)
    }
    _SampleRecycleview(msgList = msgList)
}